﻿

//根据一个点 设置角度和半径得出另外一个点
function TriangleLengthXY(coordinates: MyCoordinates, rf: number, length: number, coor: MyCoordinates)
{
    coor.x = 0;
    coor.y = 0;
    if (rf == 90)
    {
        coor.x = coordinates.x;
        coor.y = coordinates.y + length;
    } else if (rf == 0 || rf == 360)
    {
        coor.x = coordinates.x + length;
        coor.y = coordinates.y;
    } else if (rf == 180)
    {
        coor.x = coordinates.x - length;
        coor.y = coordinates.y;
    } else if (rf == 270)
    {
        coor.x = coordinates.x;
        coor.y = coordinates.y - length;
    } else
    {
        let s = (rf / 90) | 0;
        let rfs = rf - s * 90;
        let x: number;
        let y: number;
        let a = rfs / 180 * Math.PI;
        x = Math.cos(a) * length;
        y = Math.sin(a) * length;

        switch (s)
        {
            case 0:
                coor.x = x;
                coor.y = y;
                break;
            case 1:
                coor.x = -y;
                coor.y = x;
                break;
            case 2:
                coor.x = -x;
                coor.y = -y;
                break;
            case 3:
                coor.x = y;
                coor.y = -x;
                break;
        }

        coor.x += coordinates.x;
        coor.y += coordinates.y;
    }

    return coor;
}
//通过一个原点和起始点加上偏转角度得到另外一个点
function Triangrf(origin: MyCoordinates, start: MyCoordinates, rf: number, coor: MyCoordinates)
{
    let length = PointDistance(origin, start, coor);
    let orRf = TriangleRadii(origin, start, coor);
    orRf = BiasAngleCount(rf + orRf);
    return TriangleLengthXY(origin, orRf, length, coor);
}
//偏移值越界计算
function BiasAngleCount(angle)
{
    if (angle >= 360)
    {
        angle -= 360;
    }
    if (angle < 0)
    {
        angle += 360;
    }
    return angle;
}
//以开始点为圆点  计算另一个点的角度
function TriangleRadii(startDoc: MyCoordinates, endDoc: MyCoordinates, coor: MyCoordinates)
{

    let doc = endDoc.subtraction(startDoc, coor);
    if (doc.x > 0 && doc.y > 0)
    {
        return Math.atan(doc.y / doc.x) / Math.PI * 180;
    } else if (doc.x < 0 && doc.y > 0)
    {
        return Math.atan((-doc.x) / doc.y) / Math.PI * 180 + 90;
    } else if (doc.x < 0 && doc.y < 0)
    {
        return Math.atan((-doc.y) / (-doc.x)) / Math.PI * 180 + 180;
    } else if (doc.x > 0 && doc.y < 0)
    {
        return Math.atan(doc.x / (-doc.y)) / Math.PI * 180 + 270;
    } else if (doc.x >= 0 && doc.y == 0)
    {
        return 0;
    } else if (doc.x == 0 && doc.y > 0)
    {
        return 90;
    } else if (doc.x < 0 && doc.y == 0)
    {
        return 180;
    } else if (doc.x == 0 && doc.y < 0)
    {
        return 270;
    }
    return 0;

}
//计算两点距离
function PointDistance(startDoc: MyCoordinates, endDoc: MyCoordinates, coor: MyCoordinates)
{
    let doc = endDoc.subtraction(startDoc, coor);
    return Math.sqrt(Math.abs(doc.x * doc.x) + Math.abs(doc.y * doc.y));
}
//颜色转换
function ColorTransition(color: string)
{

    if (color.length == 6)
    {
        let r = parseInt("0x" + color.substr(0, 2)) / 255;
        let g = parseInt("0x" + color.substr(2, 2)) / 255;
        let b = parseInt("0x" + color.substr(4, 2)) / 255;
        return new lighttool.spriteColor(r, g, b, 1)
    }
}
//随机数计算
function Random(start: number, end: number)
{
    return Math.round(Math.random() * (end - start)) + start;
}


//点
class MyCoordinates
{
    x: number;
    y: number;

    constructor(x: number, y: number)
    {
        this.x = x;
        this.y = y;
    }

    subtraction(var1: MyCoordinates, coor: MyCoordinates)
    {
        coor.x = this.x - var1.x;
        coor.y = this.y - var1.y;
        return coor;
    }

    isCoordinates(var1: MyCoordinates)
    {
        return this.x == var1.x && this.y == var1.y;
    }

}

